package com.puman.watchtrade.fragment.home.imageLoader;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.puman.watchtrade.MyApplication;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.imageLoader.PhotoFolderFragment.OnPageLodingClickListener;
import com.puman.watchtrade.fragment.home.imageLoader.PhotoFragment.OnPhotoSelectClickListener;
import com.puman.watchtrade.fragment.home.imageLoader.bean.PhotoInfo;
import com.puman.watchtrade.fragment.home.imageLoader.bean.PhotoSerializable;
import com.puman.watchtrade.fragment.home.imageLoader.util.CheckImageLoaderConfiguration;

/**
 * @title SelectPhotoActivity.java
 * @package com.centaline.mhc.activity.friendGroupActivity
 */
public class SelectPhotoActivity extends FragmentActivity implements
		OnPageLodingClickListener, OnPhotoSelectClickListener {

	private PhotoFolderFragment photoFolderFragment;

	// private Button btnback,btnright;

	private TextView title;

	private List<PhotoInfo> hasList;

	private FragmentManager manager;
	public boolean isFrist = true;
	private int backInt = 0;
	private static SelectPhotoActivity selectPhotoActivity;
	static SelectPhotoActivity getSelectPhotoActivity(){
		return selectPhotoActivity ;
	}
	/**
	 * 已选择图片数量
	 */
	private int count;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		selectPhotoActivity = this;
		setContentView(R.layout.activity_selectphoto);

		getWindowManager().getDefaultDisplay().getMetrics(
				MyApplication.getDisplayMetrics());

		count = getIntent().getIntExtra("count", 0);

		manager = getSupportFragmentManager();

		hasList = new ArrayList<PhotoInfo>();

		// btnback = (Button)findViewById(R.id.btnback);
		// btnright = (Button)findViewById(R.id.btnright);
		findViewById(R.id.back_relative).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
//						Log.i("curry", msg);
						if (backInt != 0) {
							backInt = 0;
							isFrist = false;
							FragmentTransaction transaction = manager
									.beginTransaction();
							transaction.show(photoFolderFragment).commit();
							manager.popBackStack(0, 0);
						} else {
							finish();
						}
					}
				});
		findViewById(R.id.cancle_btn_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		title = (TextView) findViewById(R.id.title);
		// title.setText("请选择相册");
		title.setText("添加照片");
		photoFolderFragment = new PhotoFolderFragment();

		if (isFrist) {
			photoFolderFragment = new PhotoFolderFragment(true);
		} else {
			photoFolderFragment = new PhotoFolderFragment();
		}
		// photoFolderFragment = new PhotoFolderFragment();

		FragmentTransaction transaction = manager.beginTransaction();
		transaction.add(R.id.body, photoFolderFragment);
		transaction.addToBackStack(null);
		// Commit the transaction
		transaction.commit();
		// }

	}


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		CheckImageLoaderConfiguration.checkImageLoaderConfiguration(this);
	}

	@Override
	public void onPageLodingClickListener(List<PhotoInfo> list) {
		// TODO Auto-generated method stub
		// title.setText("添加照片");
		FragmentTransaction transaction = manager.beginTransaction();
		PhotoFragment photoFragment = new PhotoFragment();
		Bundle args = new Bundle();
		PhotoSerializable photoSerializable = new PhotoSerializable();
		for (PhotoInfo photoInfoBean : list) {
			photoInfoBean.setChoose(false);
		}
		photoSerializable.setList(list);
		args.putInt("count", count);
		args.putSerializable("list", photoSerializable);
		photoFragment.setArguments(args);
		transaction = manager.beginTransaction();
		transaction.hide(photoFolderFragment).commit();
		transaction = manager.beginTransaction();
		transaction.add(R.id.body, photoFragment);
		transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		transaction.addToBackStack(null);
		// Commit the transaction
		transaction.commit();
		backInt++;
	}

	@Override
	public void onPhotoSelectClickListener(List<PhotoInfo> list) {
		// TODO Auto-generated method stub
		hasList.clear();
		for (PhotoInfo photoInfoBean : list) {
			if (photoInfoBean.isChoose()) {
				hasList.add(photoInfoBean);
			}
		}
		// title.setText("已选择" + hasList.size() + "张");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && backInt == 0) {
			if (isFrist) {
				isFrist = false;
				FragmentTransaction transaction = manager.beginTransaction();
				transaction.show(photoFolderFragment).commit();
				manager.popBackStack(0, 0);
			} else {
				finish();
			}
		} else if (keyCode == KeyEvent.KEYCODE_BACK && backInt == 1) {
			backInt--;
			hasList.clear();
			// title.setText("请选择相册");
			title.setText("添加照片");
			FragmentTransaction transaction = manager.beginTransaction();
			transaction.show(photoFolderFragment).commit();
			manager.popBackStack(0, 0);
		}
		return false;
	}
}
